Skip to main content
ICT
Lesson A15 - ArrayList
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

LAB ASSIGNMENT A15.2 page 10 of 11

Permutations

Assignment:

  1. Write a program that produces random permutations of the numbers 1 to 10. (Note: “Permutation” is a mathematical name for an arrangement.) For example, there are six permutations of the numbers 1,2,3: 123, 132, 231, 213, 312, and 321.

  2. To generate a random permutation, you need to fill an ArrayList with the numbers 1 to 10 so that no two entries of the array have the same contents. You could do it by brute force, by calling Random.nextInt() until it produces a value that is not yet in the array. Instead, you should implement a smarter method. Make a second ArrayList and fill it with the numbers 1 to 10. Then pick one of those at random, remove it, and append it to the permutation ArrayList. Repeat ten times.

  3. Implement a class PermutationGenerator with the following method:

    ArrayList      nextPermutation

Instructions:

  1. Turn in your source code and a printed run output.

  2. The run output will consist of 10 lists of random permutations of the number 1 to 10. Example output is shown below:

Random Permutation List Generator

List  1:  4  6  8  1  9  7 10  5  3  2
List  2:  6  8  1  7  3  4  9 10  5  2
List  3:  2  4  9  6  8  1 10  5  7  3
List  4:  8  5  4  3  2  9  6  7  1 10
List  5: 10  3  2  6  8  9  5  7  4  1
List  6:  9 10  3  2  1  5  6  8  4  7
List  7:  3  8  5  9  4  2 10  1  6  7
List  8:  3  2  4  5  7  6  9  8 10  1
List  9:  4  1  5 10  8  3  6  2  7  9
List 10:  3  5  2  4  1  7  9  6  8 10

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.